package com.liuya.safe.tree;

import com.liuya.base.BaseModel;
import com.liuya.common.StringUtil;
import com.liuya.common.WebUtil;
import com.liuya.safe.model.SafePrivilege;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 树操作基础类
 *
 * @author liuya
 * @date 20160912
 */
public abstract class AbstractTreeHandler {
    /**
     * 默认树的主页面
     */
    public static final String DEFAULT_TREE_MAIN_PAGE = "common/treeTemplate";
    /**
     * 默认复制树节点页面
     */
    public static final String DEFAULT_COPY_PAGE = "common/copyNode";

    private String treeUrl = DEFAULT_TREE_MAIN_PAGE;

    public abstract String getPageTitle();

    public abstract String getCreatePage();

    public String getTreePage() {
        return treeUrl;
    }

    public void setTreePage(String treeJsp) {
        this.treeUrl = treeJsp;
    }

    public abstract String getNodeName(BaseModel node);

    public abstract Collection getAllNodes();

    public String getTree() {
        StringBuffer buff = new StringBuffer();
        buff.append("[");
        Collection coll = getAllNodes();
        List list = new ArrayList();
        list.addAll(coll);
        if (getTreeType().equals("privilege")) {
            Collections.sort(list, new Comparator() {
                public int compare(Object arg0, Object arg1) {
                    SafePrivilege n0 = (SafePrivilege) arg0;
                    SafePrivilege n1 = (SafePrivilege) arg1;
                    return n0.getOrderNum() - n1.getOrderNum();
                }
            });
        }
        int i = 0;
        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            BaseModel node = (BaseModel) iter.next();
            if (i > 0) {
                buff.append(",\r\n");
            }
            buff.append("{id:'")
                    .append(node.getId())
                    .append("',name:\"")
                    .append(getNodeName(node))
                    .append("\",pId:'")
                    .append(node.getPid())
                    .append("'")
                    .append(",isLeaf:'")
                    .append(node.getIsLeaf() ? "1" : "0")
                    .append("'")
                    .append(node.getIsLeaf() ? "" : ",type:'group',iconSkin:'group'")
                    .append("}");
            i++;
        }
        buff.append("]");
        return buff.toString();
    }

    public abstract BaseModel getNode(HttpServletRequest req);

    public abstract BaseModel getNodeByName(String name);

    @Transactional
    public abstract boolean addNode(Object node);

    public String getId(HttpServletRequest req) {
        // id values are n0, n1...
        String strId = req.getParameter("id");
        if (StringUtil.isEmpty(strId)) {
            return "0";
        } else {
            return strId.substring(1).trim();
        }
    }

    @Transactional
    public abstract boolean deleteNode(String id);

    @Transactional
    public abstract boolean updateNode(Object node);

    public int getPosition(HttpServletRequest req) {
        return WebUtil.getIntParameter(req, "position", 0);
    }

    public String getParentId(HttpServletRequest req) {
        // parentId values are n0, n1...
        String strId = req.getParameter("parentId");
        if (StringUtil.isEmpty(strId)) {
            return "0";
        } else {
            return strId.substring(1);
        }
    }

    @Transactional
    public abstract boolean moveNode(String parentId, String id, int position);

    public abstract String getContextMenuPage();

    public abstract BaseModel getNodeById(String id);

    public abstract String getModifyPage();

    public abstract String getTreeType();

    public void movePrivilege(HttpServletRequest req) {
    }

    public String getCopyPage() {
        return DEFAULT_COPY_PAGE;
    }

    public abstract boolean copyNode(Object node);
}
